﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
using Associations;
using System.IO;

namespace JTS_Archiver
{
    public partial class settings : Form
    {
        private Form1 m_parent;
        public settings(Form1 frm1)
        {
            InitializeComponent();
            m_parent = frm1;
        }

        public bool IsElevated
        {
            get
            {
                return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
            }
        }

        public bool addAssoc(string ext)
        {
            AF_FileAssociator assoc = new AF_FileAssociator(ext);
            string ico = Path.GetDirectoryName(Application.ExecutablePath)+"\\icons\\"+ext+".ico";
            if (File.Exists(ico))
            {
                assoc.Create("JTS_Archiver", ext.ToUpper() + " Archive", new ProgramIcon(ico), new ExecApplication(Application.ExecutablePath), new OpenWithList(new string[] { "JTS_Archiver" }));
                return true;
            }
            else
            {
                MessageBox.Show("Unable to locate icon file for "+ext+" file association! Please check to ensure your installation is not corrupt!", "Error Associating Files", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        public void delAssoc(string ext)
        {
            AF_FileAssociator assoc = new AF_FileAssociator(ext);
            if (assoc.Exists)
            {
                if (assoc.Executable.Path == Application.ExecutablePath)
                {
                    assoc.Delete();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!this.IsElevated)
            {
                if (MessageBox.Show("You need to run JTS Archiver as an Administrator in order to update the file associations." + Environment.NewLine + Environment.NewLine + "Would you like to run as Administrator now?", "Need Admin rights!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    ProcessStartInfo startInfo;
                    startInfo = new ProcessStartInfo();
                    startInfo.FileName = Application.ExecutablePath;
                    startInfo.Arguments = string.Empty;
                    startInfo.UseShellExecute = true;
                    startInfo.Verb = "runas";
                    Process.Start(startInfo);
                    m_parent.settingsQuit();
                    Close();
                }
            }
            else
            {
                bool isErrors = false;
                for (int itmno = 0; itmno < checkedListBox1.Items.Count; itmno++)
                {
                    string ext = checkedListBox1.Items[itmno].ToString();
                    CheckState chkd = checkedListBox1.GetItemCheckState(itmno);
                    if (chkd == CheckState.Checked)
                    {
                        if (addAssoc(ext) == false)
                        {
                            isErrors = true;
                        }
                    }
                    else
                    {
                        delAssoc(ext);
                    }
                }
                if (isErrors == false)
                {
                    MessageBox.Show("Your selected filetypes has been successfully associated with JTS Archiver.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
            }
        }
    }
}
